package com.cloudOffice.service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cloudOffice.model.process.Process;
import com.cloudOffice.model.process.ProcessTemplate;
import com.cloudOffice.model.system.SysUser;
import com.cloudOffice.service.MessageService;
import com.cloudOffice.service.OaProcessService;
import com.cloudOffice.service.OaProcessTemplateService;
import com.cloudOffice.service.SysUserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service
public class MessageServiceImpl implements MessageService {
	
	@Resource
	private OaProcessService processService;
	@Resource
	private SysUserService sysUserService;
	@Resource
	private OaProcessTemplateService processTemplateService;
	@Resource
	private WxMpService wxMpService;
	
	//推送待审批人员
	@Override
	public void pushPendingMessage(Long processId, Long userId, String taskId) {
		//查询流程信息
		Process process = processService.getById(processId);
		//根据userid查询要推送人信息
		SysUser sysUser = sysUserService.getById(userId);
		//查询审批模板信息
		ProcessTemplate processTemplate = processTemplateService.getById(process.getProcessTemplateId());
		//获取提交审批人的信息
		SysUser submitSysUser = sysUserService.getById(process.getUserId());
		
		//获取要给的消息人的openid
		String openId = sysUser.getOpenId();
		if (StringUtils.isEmpty(openId)) {
			//TODO 测试时添加默认值
			openId = "#";
		}
		//设置消息发送信息
		WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
				//给谁发送消息，openid值
				.toUser(openId)
				//创建模板信息的id值
				.templateId("6LHiCVD5VqQ7k-bzdWzHbItaYZ4SeRk93xsL74y4c8E")
				//点击消息，跳转的地址
				.url("http://ggkt1.vipgz1.91tunnel.com/#/show/" + processId + "/" + taskId)
				.build();
		
		JSONObject jsonObject = JSON.parseObject(process.getFormValues());
		JSONObject formShowData = jsonObject.getJSONObject("formShowData");
		StringBuilder content = new StringBuilder();
		for (Map.Entry entry : formShowData.entrySet()) {
			content.append(entry.getKey()).append("：").append(entry.getValue()).append("\n ");
		}
		
		//设置模板里面参数值
		templateMessage
				.addData(new WxMpTemplateData("first",
						submitSysUser.getName() + "提交" + processTemplate.getName() + ",请注意查看", "#272727"));
		templateMessage.addData(new WxMpTemplateData("keyword1", process.getProcessCode(), "#272727"));
		templateMessage.addData(new WxMpTemplateData("keyword2", new DateTime(process.getCreateTime()).toString("yyyy" +
				"-MM-dd HH:mm:ss"), "#272727"));
		templateMessage.addData(new WxMpTemplateData("content", content.toString(), "#272727"));
		
		//调用方法发送
		try {
			String msg = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
			System.out.println(msg);
		} catch (WxErrorException e) {
			log.error("消息发送失败", e);
			throw new RuntimeException(e);
		}
	}
}
